﻿using Protocol.Code;
using Script.Framework;
using Script.Scene;
using Script.Scene.Msg;
using Script.UI;
using Script.UI.Msg;
using UnityEngine;

namespace Script.Net.Impl
{
    public class AccountHandler : HandlerBase
    {
        public override void OnReceive(int subCode, object value)
        {
            switch (subCode)
            {
                case AccountCode.LOGIN:
                    loginResponse((int) value);
                    break;
                case AccountCode.REGIST_SRES:
                    registerResponse((int) value);
                    break;
            }
        }

        private readonly PromptMsg promptMsg = new PromptMsg();

        /// <summary>
        /// 登录响应
        /// </summary>
        private void loginResponse(int value)
        {
            switch (value)
            {
                case 0:
                    // 跳转场景
                    Debug.Log("登录成功跳转场景");
                    LoadSceneMsg msg = new LoadSceneMsg(1, () =>
                    {
                        Debug.Log("场景加载完成");
                        // 向服务器获取自身信息
                        SocketMsg socketMsg = new SocketMsg(OpCode.USER, UserCode.Get_INFO_CREQ, null);
                        Dispatch(AreaCode.NET, 0, socketMsg);
                    });

                    Dispatch(AreaCode.SCENE, SceneEvent.LOAD_SCENE, msg);
                    break;
                case -1:
                    // 登录错误
                    promptMsg.Change("账号不存在", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
                case -2:
                    // 登录错误
                    promptMsg.Change("账号在线", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
                case -3:
                    // 登录错误
                    promptMsg.Change("密码输入不匹配", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
            }
        }

        /// <summary>
        /// 注册响应
        /// </summary>
        /// <param name="value"></param>
        private void registerResponse(int value)
        {
            switch (value)
            {
                case 0:
                    promptMsg.Change("注册成功", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
                case -1:
                    // 登录错误
                    promptMsg.Change("账号已存在", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
                case -2:
                    // 登录错误
                    promptMsg.Change("账号输入不合法", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
                case -3:
                    // 登录错误
                    promptMsg.Change("密码输入不合法", Color.red);
                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);
                    break;
            }
        }
    }
}